//
//  ViewController.m
//  UI14_UIcollectionView
//
//  Created by Hanlingyu on 16/8/2.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ViewController.h"
#define kCollectionViewReuse @"reuse"
#define kCollectionViewReusableHeader @"headerReuse"
#define kCollectionViewReusableFooter @"footerReuse"

@interface ViewController ()
<
    UICollectionViewDelegate,
    UICollectionViewDataSource
>
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 1.UICollectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    NSLog(@"layout.itemSize--------%@", NSStringFromCGSize(layout.itemSize));
    // 默认itemSize为{50, 50}
    // 1. itemSize
    layout.itemSize = CGSizeMake(CGRectGetWidth(self.view.bounds) / 2 - 7.5, CGRectGetWidth(self.view.bounds) / 2 - 7.5);
    // 2. scrollDirection 滚动方向
    //UICollectionViewScrollDirectionVertical 竖直滚动
    //UICollectionViewScrollDirectionHorizontal 水平滚动
    // 滚动方向改变, 会交换列和行(包括区)
//    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 3. minimumLineSpacing 最小行间距
    layout.minimumLineSpacing = 5;
    // 4.minimumInteritemSpacing 最小列间距
    layout.minimumInteritemSpacing = 5;
    // 5. sectionInset
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    // 6. 区头大小 (横向滚动时, 设置宽, 纵向滚动时, 设置高)
//    layout.headerReferenceSize = CGSizeMake(100, 0);
    layout.headerReferenceSize = CGSizeMake(0, 100);
    // 7. 区尾大小 (横向滚动时, 设置宽, 纵向滚动时, 设置高)
    layout.footerReferenceSize = CGSizeMake(50, 50);
    // 8. iOS9之后, 设置区头悬浮效果
    layout.sectionHeadersPinToVisibleBounds = YES;
    // 9. 设置区尾悬浮效果
    layout.sectionFootersPinToVisibleBounds = YES;
    
    
    // UICollectionView创建
    // 参数1. frame
    // 参数2. 填入不能为空的layout(布局)
    UICollectionView *myCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    myCollectionView.delegate = self;
    myCollectionView.dataSource = self;
    [self.view addSubview:myCollectionView];
    [myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewReuse];
    
    [myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewReusableHeader];
    [myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCollectionViewReusableFooter];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 10;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReuse forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor greenColor];
    
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewReusableHeader forIndexPath:indexPath];
        reusableView.backgroundColor = [UIColor redColor];
        return reusableView;
    } else {
        UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCollectionViewReusableFooter forIndexPath:indexPath];
        reusableView.backgroundColor = [UIColor yellowColor];
        return reusableView;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
